<template>
  <div class="login-container">
    <!-- 左侧背景与装饰元素 -->
    <div class="login-bg">
      <div class="bg-shape shape-1"></div>
      <div class="bg-shape shape-2"></div>
      <div class="bg-shape shape-3"></div>
      <div class="bg-content">
        <h1 class="logo">🐌<span>MD</span></h1>
        <p class="slogan">智能在线文本编辑</p>
      </div>
    </div>

    <!-- 右侧登录表单 -->
    <div class="login-form">
      <div class="form-tabs">
        <button class="tab-btn" :class="{ active: currentTab === 'teacher' }" @click="switchTab('teacher')">
          AIMD
        </button>
      </div>
      <!-- 用户登录表单 -->
      <div v-show="currentTab === 'teacher'" class="form-content">
        <h2>用户登录</h2>
        <div class="form-group">
          <label for="teacher-account">账号</label>
          <div class="input-group">
            <i class="icon-user"></i>
            <input type="text" id="teacher-account" v-model="teacherForm.account" placeholder="输入教师账号">
          </div>
        </div>
        <div class="form-group">
          <label for="teacher-password">密码</label>
          <div class="input-group">
            <i class="icon-lock"></i>
            <input type="password" id="teacher-password" v-model="teacherForm.password" placeholder="输入密码">
            <i class="icon-eye" @click="toggleTeacherPassword"></i>
          </div>
        </div>
        <div class="form-remember">
          <label class="checkbox">
            <input type="checkbox" v-model="teacherForm.remember">
            <span class="checkbox-custom"></span>
            记住密码
          </label>
          <!-- 注册按钮移至此处 -->
          <router-link to="/register" class="forgot-password-link">
            <i class="icon-user-plus"></i> 立即注册
          </router-link>
        </div>
        <button class="login-btn" @click="handleTeacherLogin" :class="{ loading: teacherLoading }">
          <span v-if="!teacherLoading">登录</span>
          <span v-else class="loading-spinner"></span>
        </button>
      </div>

    </div>
  </div>
</template>

<script setup lang="ts">
import { useUserStore } from '@/stores/userStore';
import { ref, onMounted ,onBeforeMount} from 'vue';
import { useRouter, useRoute } from 'vue-router';
// 路由实例
const router = useRouter();
const userStroe = useUserStore(); // 实例化Pinia存储

// 表单状态
const currentTab = ref('teacher');
const teacherForm = ref({
  account: '',
  password: '',
  remember: false
});
const teacherPasswordType = ref('password');
const teacherLoading = ref(false);

// 切换密码可见性
const toggleTeacherPassword = () => {
  teacherPasswordType.value = teacherPasswordType.value === 'password' ? 'text' : 'password';
};

// 切换标签页
const switchTab = (tab: string) => {
  currentTab.value = tab;
  // 平滑滚动到表单顶部
  document.querySelector('.login-form')?.scrollTo({ top: 0, behavior: 'smooth' });
};

// 处理登录
const handleTeacherLogin = async () => {


  router.push('/teacher');
  // 简单表单验证
  if (!teacherForm.value.account || !teacherForm.value.password) {
    alert('请输入用户名和密码');
    return;
  }

  teacherLoading.value = true;
  try {
    // 登录请求
    const user = {
      account: teacherForm.value.account,
      password: teacherForm.value.password
    }
    console.log("开始请求登录-----");

    await userStroe.login(user) ;
    // 通过userStore.user判断是否登录成功
    console.log("返回结果-----", userStroe.response);
    if (userStroe.response.code=== 200) {
      alert('登录成功');
      if(userStroe.user.role== 0){
      router.push('/teacher');
      }else{
        router.push('/student');
      }

    } else {
      alert('账号或密码错误');
    }
  } catch (error: any) {
    console.error('登录请求失败', error);
    alert(error.response?.data?.message || '网络错误，请稍后再试');
  } finally {
    teacherLoading.value = false;
  }
};

//页面一键登录
const handleQuickLogin = async ()=>{
  console.log("当前登录角色：",userStroe.user.role);

  console.log("开始一键登录---------");
  console.log("当前用户角色：",userStroe.user.role);
  
  const user = {
      account: userStroe.user.account,
    }
  await userStroe.login(user);

      if (userStroe.response.code === 200) {
      console.log('已登录');

      if(userStroe.user.role== 0){
      router.push('/teacher');
      }else{
        router.push('/student');
      }
}}


// 页面加载时自动聚焦到用户名输入框
onMounted(() => {
  const input = document.querySelector('input[type="text"]');
  if (input) {
    input.focus();
  }

});

onBeforeMount(()=> {
  handleQuickLogin();
})
</script>

<style scoped>
/* 基础样式重置 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', sans-serif;
}

body {
  min-height: 100vh;
  background-color: #f5f7fa;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #333;
  overflow-x: hidden;
}

.login-container {
  width: 100%;
  max-width: 1200px;
  height: 80vh;
  max-height: 600px;
  border-radius: 24px;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  display: flex;
  background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
}

/* 左侧背景样式 */
.login-bg {
  flex: 1;
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  padding: 60px 80px 80px;
  color: white;
  background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  overflow: hidden;
}

.login-bg::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url("data:image/svg+xml,%3Csvg width='100' height='100' viewBox='0 0 100 100' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M11 18c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm48 25c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm-43-7c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zm63 31c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM34 90c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zm56-76c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM12 86c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm28-65c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm23-11c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-6 60c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm29 22c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zM32 63c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm57-13c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-9-21c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM60 91c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM35 41c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM12 60c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2z' fill='%23ffffff' fill-opacity='0.05' fill-rule='evenodd'/%3E%3C/svg%3E") no-repeat;
  background-size: cover;
  opacity: 0.3;
}

.bg-shape {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  z-index: 1;
}

.shape-1 {
  width: 300px;
  height: 300px;
  top: -150px;
  right: -150px;
}

.shape-2 {
  width: 200px;
  height: 200px;
  bottom: -100px;
  left: -100px;
}

.shape-3 {
  width: 100px;
  height: 100px;
  top: 20%;
  left: 10%;
}

.bg-content {
  position: relative;
  z-index: 2;
}

.logo {
  font-size: 36px;
  font-weight: 700;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
}

.logo span {
  color: #f97316;
}

.slogan {
  font-size: 18px;
  opacity: 0.9;
  max-width: 80%;
}

/* 右侧表单样式 */
.login-form {
  flex: 1;
  background: white;
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  position: relative;
}

.form-tabs {
  display: flex;
  margin-bottom: 30px;
  border-bottom: 2px solid #e5e7eb;
  position: relative;
}

.tab-btn {
  background: none;
  border: none;
  padding: 12px 20px;
  font-size: 16px;
  font-weight: 500;
  color: #6b7280;
  cursor: pointer;
  position: relative;
  transition: all 0.3s ease;
}

.tab-btn.active {
  color: #4f46e5;
  font-weight: 600;
}

.tab-btn.active::after {
  content: "";
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 100%;
  height: 2px;
  background: linear-gradient(90deg, #4f46e5, #7c3aed);
}

.form-content {
  flex: 1;
}

h2 {
  font-size: 28px;
  font-weight: 700;
  color: #1f2937;
  margin-bottom: 30px;
}

.form-group {
  margin-bottom: 24px;
}

label {
  display: block;
  margin-bottom: 8px;
  font-size: 14px;
  font-weight: 500;
  color: #4b5563;
}

/* 输入框样式 */
.input-group {
  position: relative;
  display: flex;
  align-items: center;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  overflow: hidden;
  transition: all 0.3s ease;
}

.input-group:focus-within {
  border-color: #4f46e5;
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}

.input-group i {
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #9ca3af;
  background-color: #f3f4f6;
  border-right: 1px solid #e5e7eb;
  font-size: 18px;
  transition: background-color 0.2s ease;
}

.input-group input {
  flex: 1;
  height: 48px;
  padding: 0 16px;
  border: none;
  outline: none;
  font-size: 16px;
  color: #374151;
  background-color: transparent;
  transition: background-color 0.2s ease;
}

.input-group input::placeholder {
  color: #9ca3af;
}

.input-group input:focus {
  background-color: #f9fafb;
}

.input-group .icon-eye {
  border-left: 1px solid #e5e7eb;
  border-right: none;
  cursor: pointer;
}

.input-group .icon-eye:hover {
  color: #4f46e5;
}

/* 复选框样式 */
.form-remember {
  display: flex;
  align-items: center;
  margin-bottom: 24px;
  justify-content: space-between;/* 两端对齐 */
}

.checkbox {
  display: flex;
  align-items: center;
  cursor: pointer;
  user-select: none;
}

.checkbox input {
  position: absolute;
  opacity: 0;
  cursor: pointer;
  height: 0;
  width: 0;
}

.checkbox-custom {
  position: relative;
  width: 18px;
  height: 18px;
  border: 2px solid #d1d5db;
  border-radius: 4px;
  margin-right: 8px;
  transition: all 0.2s ease;
}

.checkbox input:checked~.checkbox-custom {
  background-color: #4f46e5;
  border-color: #4f46e5;
}

.checkbox input:checked~.checkbox-custom::after {
  content: "";
  position: absolute;
  left: 5px;
  top: 1px;
  width: 4px;
  height: 9px;
  border: solid white;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}

/* 登录按钮样式 */
.login-btn {
  width: 100%;
  height: 48px;
  background: linear-gradient(90deg, #4f46e5, #7c3aed);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-btn:hover {
  background: linear-gradient(90deg, #4338ca, #6d28d9);
  transform: translateY(-1px);
}

.login-btn:active {
  transform: translateY(1px);
}

.login-btn.loading {
  cursor: not-allowed;
  opacity: 0.8;
}

.loading-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid white;
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* 底部链接样式 */
.form-footer {
  text-align: center;
  margin-top: 24px;
}

.form-footer p {
  font-size: 14px;
  color: #6b7280;
}

.link {
  color: #4f46e5;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.2s ease;
}

.link:hover {
  color: #3b82f6;
  text-decoration: underline;
}


/* 响应式设计 */
@media (max-width: 768px) {
  .login-container {
    flex-direction: column;
    max-height: none;
    height: auto;
    margin: 20px;
  }

  .login-bg {
    padding: 40px;
    height: 300px;
  }

  .login-form {
    padding: 30px;
  }

  .logo {
    font-size: 30px;
  }

  .slogan {
    font-size: 16px;
  }

  h2 {
    font-size: 24px;
  }
}

/* 注册按钮容器样式（增强布局稳定性） */
.register-btn-container {
  margin-top: 20px;
  text-align: center;
  position: relative;
  /* 防止被浮动元素覆盖 */
  z-index: 10;
  /* 提升图层优先级 */
}

/* 注册按钮主体样式（明确显示属性） */
.register-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 12px 24px;
  background-color: #f8fafc;
  /* 临时背景色便于调试 */
  border: 2px solid #4f46e5;
  border-radius: 10px;
  color: #4f46e5;
  font-size: 15px;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  transition: all 0.3s ease;
  min-width: 160px;
  /* 防止内容挤压 */
  box-shadow: 0 2px 5px rgba(79, 70, 229, 0.1);
  /* 增加阴影提升可见性 */
}

/* 图标样式 */
.register-btn i {
  margin-right: 8px;
  font-size: 18px;
}

/* 交互状态样式（增强视觉反馈） */
.register-btn:hover {
  background-color: #4f46e5;
  color: white;
  transform: translateY(-3px);
  box-shadow: 0 5px 15px rgba(79, 70, 229, 0.25);
}

.register-btn:active {
  transform: translateY(0);
  box-shadow: 0 2px 5px rgba(79, 70, 229, 0.1);
}

/* 响应式适配（移动端优化） */
@media (max-width: 768px) {
  .register-btn {
    padding: 10px 20px;
    font-size: 14px;
  }
}

/* 忘记密码/注册链接样式 */
.forgot-password-link {
  display: inline-flex;
  align-items: center;
  color: #4f46e5;
  font-size: 14px;
  font-weight: 500;
  text-decoration: none;
  transition: color 0.2s ease;
}

.forgot-password-link i {
  margin-right: 4px;
  font-size: 16px;
}

.forgot-password-link:hover {
  color: #3b82f6;
  text-decoration: underline;
}

/* 移除底部注册提示 */
.form-footer {
  display: none;
}
</style>
